package com.durian.base.rxhttp.download

import android.content.Context
import android.net.Uri
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream

/**
 * 说明：ExpandOutputStream
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2021/10/20 10:02 下午
 * <p/>
 * 版本：version 1.0
 */
class ExpandOutputStream<out T>(
    val expand: T,
    val os: OutputStream
) {
    override fun toString(): String = "($expand, $os)"
}

internal fun File.toOutputStream(append: Boolean = false) =
    ExpandOutputStream(absolutePath, FileOutputStream(this, append))

internal fun Uri.toOutputStream(
    context: Context,
    append: Boolean = false
): ExpandOutputStream<Uri> {
    val os = context.contentResolver.openOutputStream(this, if (append) "wa" else "w")!!
    return ExpandOutputStream(this, os)
}